$(function(){
	$(".getCode").on("click",function(){
		var num=$("#num").val();
		var yzm=$("#yzm").val();
		var onlyKey=$("#onlyKey").val();
		if(num==null||num==""){
			showTip("手机号不能忘了!");
		}else if(!(/^1[3|4|5|7|8]\d{9}$/.test(num))){
			showTip("手机号输入错误!");
		}else if(yzm==null||yzm==""){
			showTip("验证码不能忘了!");
		}else{
			getCode(num,yzm,onlyKey);
		}
	})
	$(".login").on("click",function(){
		var num=$("#num").val();
		var yzm=$("#yzm").val();
		var dxyzm=$("#dxyzm").val();
		var mm=$("#mm").val();
		var mmAgain=$("#mmAgain").val();
		var onlyKey=$("#onlyKey").val();
		var hiddenreturnUrl=$("#hiddenreturnUrl").val();
		if(num==null||num==""){
			showTip("手机号不能忘了!");
		}else if(!(/^1[3|4|5|7|8]\d{9}$/.test(num))){
			showTip("手机号输入错误!");
		}else if(yzm==null||yzm==""){
			showTip("验证码不能忘了!");
		}else if(dxyzm==null||dxyzm==""){
			showTip("短信校验码不能忘了!");
		}else if(mm==null||mm==""){
			showTip("密码一定要哟!");
		}else if(mmAgain==null||mmAgain==""){
			showTip("请确认密码先!");
		}else if(mm!=mmAgain){
			showTip("两次密码输入不一致!");
		}else{
			register(num,yzm,dxyzm,mm,onlyKey);
		}
	})
	$(".changeYzm").on("click",function(){
		changeyzm();
	})
})
window.onload=function(){
	showyanzhengma();
}
//显示错误信息
function showTip(tip){
	var w = $(window).width()/15*0.25;
	$(".tips").html(tip);
	$(".tips").fadeIn();
	var tipWidth=-$(".tips").width()/2-w+"px";
	$(".tips").css("margin-left",tipWidth);
	setTimeout(function(){
		$(".tips").fadeOut();
	},1500);
}
//显示图形验证码
function showyanzhengma(){
	$('#yzmimg').attr("src","/captcha/code.html");
}
//更改图形验证码
function changeyzm(){
	$('#yzmimg').attr("src","/captcha/code.html");
}
//获取短信验证码
function getCode(num,yzm,onlyKey){
	console.log(21);
	var i=60;
	var timer=setInterval(function(){
		$(".getCode").html("重新发送"+i+"s");	
		$(".getCode").css("color","#7c7c7c");
		i=i-1;
		if(i<0){
			clearInterval(timer);
			$(".getCode").html("获取短信码");
			$(".getCode").css("color","#ff8181");
		}
	},1000);
	$.ajax({
		type:"POST",
		url : "/code/sendValidCode.json",
		dataType : "json",
		data : {
			"phone" :num,
			"sourseType":"f",
			"tuxingCode" : yzm,
			"commkeyid":onlyKey,
		},
		success : function(data) {
			if(data.code=="-400"){
				showTip("验证码君好像迷糊了!");
			}else if(data["code"] == "1") {	
			}else if(data["code"] == "0") {
				showTip("该手机号已注册!");
			}
		}
	})
}
//提交注册信息
function register(num,yzm,dxyzm,mm,onlyKey){
	$.ajax({
		type:"POST",
		url : "/user/register/forgotPassAction.do",
		dataType : "json",
		data : {
			"phone" :num,
			"sourseType":"f",
			"tuxingCode" : yzm,
			"passWord": mm,
			"commkeyid":onlyKey,
			"valid":dxyzm
		},
		success : function(data) {
			if(data.code=="-400"){
				showTip("验证码君好像迷糊了!");
			}else if(data["code"] == "1") {	
				location.href="/user/checkCode.html?pass="+$("#mm").val()+"&phone="+$("#num").val()+"&returnUrl="+hiddenreturnUrl;	
			}else if(data["code"] == "0") {
				showTip("该手机号已注册!");
			}else if(data["code"] == "200") {
				location.href="user/success.htm";
			}
		}
	})
}
